package net.ojbk.study.curriculumDesign.service.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.Chat;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatMessage;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatSystem;
import net.ojbk.study.curriculumDesign.common.cilentservicer.ChatStatus;
import net.ojbk.study.curriculumDesign.dao.cilentservicer.ServicerDao;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICenterCommService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/8
 * Time: 18:07
 * Description: No Description
 */
@Service("centerCommService")
public class CenterCommServiceImpl implements ICenterCommService {
    @Autowired
    @Qualifier("servicerDao")
    ServicerDao servicerDao;

    @Autowired
    @Qualifier("chatSystem")
    ChatSystem chatSystem;

    /**
     * 客服中心 是否还有 未连接的用户会话
     *
     * @return
     */
    @Override
    public boolean isHaveNewRequest() {
        return !chatSystem.getChatBeginList().isEmpty();
    }

    /**
     * 获取 客服中心 所有 未被连接的用户会话
     *
     * @return
     */
    @Override
    public List<Chat> getAllRequestChat() {
        return chatSystem.getAllBeginChat();
    }

    /**
     * 强行 切断 客户中心的 一个会话，设置其为 切断状态，并移除队列
     *
     * @param chat 带切断的会话
     */
    @Override
    public void forceCutChat(Chat chat) throws Exception {
        // 查他是那个队列
        List<Chat> inThisChatList = null;
        if (chatSystem.getChatBeginList().indexOf(chat) != -1)
            inThisChatList = chatSystem.getChatBeginList();
        else if (chatSystem.getChatingList().indexOf(chat) != -1)
            inThisChatList = chatSystem.getChatingList();
        else
            throw new Exception("此chat不存在");
        // 设置他是5状态
        chat.setStatus(ChatStatus.STATUS_CAT);
        // 队列移出
        inThisChatList.remove(chat);
    }

    /**
     * 获取 客服中心，所有正在chat的chat
     *
     * @return
     */
    @Override
    public List<Chat> getAllChating() {
        return chatSystem.getChatingList();
    }

    /**
     * 获取该 system中的 某chat的所有聊天记录
     *
     * @param chat
     * @return
     */
    @Override
    public List<ChatMessage> getChatAllMsg(Chat chat) throws Exception{
        if (chatSystem.getChatingList().indexOf(chat) == -1)
            throw new Exception("此会话不存在或未开始");
        return chat.getMsgList();
    }
}
